<template>
    <a-select @change="handleChange" @search="handleSearch" 
        :allowClear="allowClear"
        :value="value" 
        :disabled="disabled" 
        show-search 
        :not-found-content="undefined"
        :show-arrow="false"
        :mode="mode">
        <a-spin v-if="fetching" slot="notFoundContent" size="small" />
        <a-select-option v-for="(item) in dictOptions" :key="item.itemValue" :value="item.itemValue">
            {{item.itemText}}
        </a-select-option>
    </a-select>
</template>
<script>
import request from '@/utils/request'
import {StringToJson} from '@/utils/util'
export default {
    name:'LSearchSelect',
    props:{
        allowClear:{
            type:Boolean,
            default:true
        },
        value:[String,Number,Array],
        form:{
            type:Object,
            default:()=>{}
        },
        changeItem:{
            type:Object,
            default:()=>{}
        },
        parentSelect:String,
        field:String,
        param:{
            type:[String,Object],
            default:()=>{}
        },
        paramFormField:[String,Array],
        currentParam:String,
        url:String,
        disabled:{
            type:Boolean,
            default:false
        },
        replaceFields:{
            type:[Object,String],
            default:()=>{}
        },
        method:{
            type:String,
            default:'post'
        },
        mode:{
            type:String,
            default:'default'
        }


    },
    watch:{
      changeItem:{
        deep:true,
        handler() {
          if(this.changeItem  && this.parentSelect && this.changeItem.hasOwnProperty(this.parentSelect)){
              let itemJson = JSON.parse(JSON.stringify(this.changeItem))
              if(Object.getOwnPropertyNames(itemJson).length==1){
                this.form.setFieldsValue({[`${this.field}`]:''})
                this.dictOptions=[]
              }
          }
        },
      }

    },
    created() {
        if(this.replaceFields){
            this.localReplaceFields = StringToJson(this.replaceFields)
        }
        if(this.paramFormField){
            this.localParamFormField = StringToJson(this.paramFormField)
        }
        if(this.param){
            this.localParam = StringToJson(this.param)
        }
        
    },
    data(){
        return{
            dictOptions: [],
            resultOptions:[],
            timeout:null,
            currentValue:null,
            fetching:false,
            localReplaceFields:{},
            localParamFormField:[],
            localParam:{}
        }
    },
    methods: {
        handleChange(e) {
            this.fetching=false
            this.$emit('change', e);
        },
        replaceField(result){
            let that = this
      
            for(var val of result){
                let replaceObj={};
                if(that.localReplaceFields['itemFilter']){
                    replaceObj['itemFilter'] = val[that.localReplaceFields['itemFilter']]
                }else{
                    replaceObj['itemFilter'] = val['itemFilter']
                }
                if(that.localReplaceFields['itemText']){
                    replaceObj['itemText'] = val[that.localReplaceFields['itemText']]
                }else{
                    replaceObj['itemText'] = val['itemText']
                }
                if(that.localReplaceFields['itemValue']){
                    replaceObj['itemValue'] = val[that.localReplaceFields['itemValue']]
                }else{
                    replaceObj['itemValue'] = val['itemValue'] 
                }
                that.dictOptions.push(replaceObj);
            }
        },
        handleSearch(val){
            let that = this
            
            that.dictOptions=[]
            if(val){
                if (that.timeout) {
                    clearTimeout(that.timeout);
                    that.timeout = null;
                }
                that.currentValue = val
                //调用后台
                that.timeout = setTimeout(()=>{
                    if(that.url){
                        let param={}
                        if(that.localParamFormField && that.localParamFormField.length>0){
                            for (var value of that.localParamFormField) {
                                if(!that.form.getFieldValue(value)){
                                    return false
                                }
                                param[value] = that.form.getFieldValue(value)
                            }
                        }
                        param[that.currentParam] = that.currentValue
                        if(that.parentSelect && that.form.getFieldValue(that.parentSelect)){
                            param = Object.assign(param,{[`${that.parentSelect}`]:that.form.getFieldValue(that.parentSelect)})
                        }
                        if(that.localParam){
                            param = Object.assign(param,that.localParam)
                        }
                        that.fetching = true
                        if(that.method=='get'){
                            request.get(that.url,{params:param}).then((res)=>{
                                if(res.success){
                                    if(that.localReplaceFields){
                                        that.replaceField(res.result)
                                    }else{
                                        that.dictOptions = res.result
                                    }
                                }else{
                                    that.$message.warning(res.message);
                                }
                            }).finally(()=>{
                                that.fetching = false
                            })

                        }else{
                            request({url: that.url,method:that.method,data: param}).then((res)=>{
                                if(res.success){
                                    if(that.localReplaceFields){
                                        that.replaceField(res.result)
                                    }else{
                                        that.dictOptions = res.result
                                    }
                                }else{
                                    that.$message.warning(res.message);
                                }
                            }).finally(()=>{
                                that.fetching = false
                            })
                        }
                        
                    }
                    
                    
                }, 500)
            }
            
            
        }
        
    }
}
</script>